
//**************************************************************************/
// Copyright (c) 2010 Autodesk, Inc.
// All rights reserved.
//
// Use of this software is subject to the terms of the Autodesk license
// agreement provided at the time of installation or download, or which
// otherwise accompanies this software in either electronic or hard copy form.
//
//**************************************************************************/
// DESCRIPTION:
// CREATED: August 2010
//**************************************************************************/

#if defined(JAMBUILD)
#include <Mudbox/mudbox.h>
#include <MapExtractor/MapExtractorInterface.h>
#else
#include "../../include/Mudbox/mudbox.h"
#include "../../include/MapExtractor/MapExtractorInterface.h"
#endif

using namespace mudbox;

// This class is responsible to specify the location and order of the reference points on the target mesh which needs a value to be extracter. In case of a ptex extraction
// process this class will specify points on each base level face in a grid pattern.
class PtexLayout : public Layout
{
	DECLARE_CLASS;
	// These are the additional data which will be added to each reference point. Based on these data the PtexUtilizer class (which is responsible to process the result
	// of the extraction) will know where to put the data in the ptex file.
	enum
	{
		dataURes,	// coordinate of the reference point in the grid in horizontal direction
		dataVRes,	// coordinate of the reference point in the grid in horizontal direction
		dataFaceID	// ID of the ptex face
	};
	PtexLayout( void );
	// This function creates the user interface widgets which belong to this type of layout.
	virtual QWidget *UserInterface( void );
	// Main function of the class. This is only called once for a map extraction operation. This function collects the reference points on the target surface(s).
	virtual void ProcessSurfaces( QVector<SubdivisionLevel *> aSurfaces );
	// Prepares the instance for the extraction, returns the expected number of reference points (for progress indicator)
	virtual unsigned int Prepare( void );
	// When the map extraction node is saved into a mud file, this function must serialize the data members of the object.
	virtual void Serialize( Stream &s );
	// This function returns the resolution for a given target mesh.
	int TargetResolution( int iTargetIndex ) const;
	
	// This is the only one parameter of the object, it controls the resolution of the grid for each base level face.
	aenum m_eQuality;
	// This member variable controls directly the resolution of the ptex file. If the value stored in this variable is zero, then m_eQuality and the level
	// difference between the target and source surfaces are used.
	int m_iDirectResolution;

	// The remaining functions and variables are needed to caculate the adjacency information for nsided meshes. For full quad meshes
	// there is a one-to-one relationship between mudbox quads and ptex faces, so in that case the adjacency calculation is easy.
	// For more information on how ptex expects the adjacency info, see http://http://ptex.us/adjdata.html

	// This function precalculates some adjacency related data for the mesh.
	void PrepareAdjacency( const Mesh *m_pMesh );
	// This function calculates the adjacency info for an edge of a triagle mudbox face.
	unsigned int AdjacentFaceForTriangle( unsigned int iFaceIndex, unsigned int iSide, unsigned int iSegment, unsigned int &iEdge ) const;
	// This function calculates the adjacency info for a ptex face edge.
	unsigned int AdjacentFace( unsigned int iFaceID, unsigned int iSide, unsigned int &iEdge ) const;

	// Pointer to the mesh which adjacency is currently being calculated.
	const Mesh *m_pMesh;
	// The following array tells the ptex face id for a given mudbox triangle. Contains valid values only for real triangles.
	QVector<unsigned int> m_aFaceID;
	// The following array tells the mudbox triangle index for a given ptex face ID. This array is the opposite of the m_aFaceID array.
	QVector<unsigned int> m_aTriangle;
};
